﻿using Core.Domain.Entities;
using Core.Migrations.Extensions;
using FluentMigrator;

namespace Infrastructure.Persistence.Migrations.UserOrganizations;

[Migration(202510292059)]
public class CreateColumn : ForwardOnlyMigration
{
    public override void Up()
    {
        Alter.Table(typeof(UserOrganization).GetTableName())
          .InSchema(SchemaNames.Identity)
          .AddColumn("TenantId").AsString(100).Nullable().WithColumnDescription("租户标识")
          .AddColumn(MigrationExpressions.ColumnNameFor<UserOrganization>(nameof(UserOrganization.UserId)))
          .AsGuid().Nullable()
          .WithColumnDescription("用户ID")

          .AddColumn(MigrationExpressions.ColumnNameFor<UserOrganization>(nameof(UserOrganization.OrganizationId)))
          .AsGuid().Nullable()
          .WithColumnDescription("组织ID")

          .AddColumn(MigrationExpressions.ColumnNameFor<UserOrganization>(nameof(UserOrganization.Position)))
          .AsString(100).Nullable().WithColumnDescription("组织职位");

        // 2. 创建复合索引
        Create.Index("IX_UserOrganizations_UserId_OrganizationId")
               .OnTable(typeof(UserOrganization).GetTableName())
               .InSchema(SchemaNames.Identity)
               .OnColumn(nameof(UserOrganization.UserId)).Ascending()
               .OnColumn(nameof(UserOrganization.OrganizationId)).Ascending(); 

        // 3. 创建单列索引：加速按组织查询所有成员
        Create.Index("IX_UserOrganizations_OrganizationId")
               .OnTable(typeof(UserOrganization).GetTableName())
               .InSchema(SchemaNames.Identity)
               .OnColumn(nameof(UserOrganization.OrganizationId)).Ascending();
    }
}
